package com.xbts.web.controller.cloud;

import java.util.List;

import com.xbts.system.domain.CnNewsType;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.xbts.common.annotation.Log;
import com.xbts.common.core.controller.BaseController;
import com.xbts.common.core.domain.AjaxResult;
import com.xbts.common.enums.BusinessType;
import com.xbts.system.domain.CnNews;
import com.xbts.system.service.ICnNewsService;
import com.xbts.common.utils.poi.ExcelUtil;
import com.xbts.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 新闻Controller
 * @author xbts
 * @date 2023-08-03
 */
@Api("新闻")
@RestController
@RequestMapping("/system/news")
public class CnNewsController extends BaseController {

    @Autowired
    private ICnNewsService cnNewsService;
    /**
     * 查询所有新闻
     */
    @ApiOperation("查询所有新闻")
    @ApiImplicitParam(name = "newsType", value = "新闻类型", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PostMapping("/list")
    public TableDataInfo getNewsList(@RequestBody CnNews cnNews){
        startPage(cnNews);
        return getDataTable(cnNewsService.selectAllNews(cnNews));
    }

    /**
     * 展示最新十条新闻
     */
    @ApiOperation("展示最新十条新闻")
    @PreAuthorize("@ss.hasPermi('system:news:list')")
    @GetMapping("/topNews")
    public AjaxResult getNewestNews(){
        return success(cnNewsService.selectTopTenNews());
    }

    /**
     * 获取新闻详细信息
     */
    @ApiOperation("获取新闻详细信息")
    @GetMapping("/getInfo/{newsId}")
    public AjaxResult getInfo(@PathVariable("newsId") Long newsId){
        return success(cnNewsService.getInfo(newsId));
    }

    /**
     * 新增新闻内容
     */
    @ApiOperation("新增新闻内容")
    @PreAuthorize("@ss.hasPermi('system:news:add')")
    @Log(title = "新增新闻内容", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult add(@Validated @RequestBody CnNews cnNews) {
        return toAjax(cnNewsService.insertCnNews(cnNews));
    }


    /**
     * 删除新闻内容
     */
    @ApiOperation("删除新闻内容")
    @PreAuthorize("@ss.hasPermi('system:news:remove')")
    @Log(title = "删除新闻内容", businessType = BusinessType.DELETE)
    @ApiImplicitParam(name = "newsId", value = "新闻id", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PostMapping("/delete")
    public AjaxResult deleteContent(Long newsId) {
        return toAjax(cnNewsService.deleteContentById(newsId));
    }
    /**
     * 批量删除新闻内容
     */
    @ApiOperation("批量删除新闻内容")
    @PreAuthorize("@ss.hasPermi('system:news:remove')")
    @Log(title = "批量删除新闻内容", businessType = BusinessType.DELETE)
    @ApiImplicitParam(name = "ids", value = "新闻id集合", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PostMapping("/batchDelete")
    public AjaxResult batchDeleteContent(@RequestBody List<Long> ids) {
        return toAjax(cnNewsService.batchDeleteContentById(ids));
    }
    /**
     * 更新新闻内容
     */
    @ApiOperation("更新新闻内容")
    @PreAuthorize("@ss.hasPermi('system:news:edit')")
    @Log(title = "更新新闻内容", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult updateContent(@RequestBody CnNews cnNews) {
        return toAjax(cnNewsService.updateNews(cnNews));
    }
}